package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.hjq.widget.view.QMUILoadingView;
import com.qiantu.api.entity.ScanWirelessControllerBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;
import com.qiantu.phone.glide.GlideApp;

/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2019/09/22
 *    desc   : 状态数据列表
 */
public final class AddWirelessDeviceAdapter extends AppAdapter<ScanWirelessControllerBean> {

    public AddWirelessDeviceAdapter(@NonNull Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder();
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {

        private final ImageView iconDevice;
        private final TextView tvDeviceName;
        private final TextView tvDeviceId;
        private final TextView btnPreset;
        private final TextView btnAdd;
        private final TextView tvFailed;
        private final TextView btnRetry;
        private final QMUILoadingView loadingView;
        private final TextView tvAdded;

        private ViewHolder() {
            super(R.layout.item_add_wireless_device);
            iconDevice = findViewById(R.id.icon_device);
            tvDeviceName = findViewById(R.id.tv_device_name);
            tvDeviceId = findViewById(R.id.tv_device_id);
            btnPreset = findViewById(R.id.btn_preset);
            btnAdd = findViewById(R.id.btn_add);
            btnRetry = findViewById(R.id.btn_retry);
            tvFailed = findViewById(R.id.tv_failed);
            loadingView = findViewById(R.id.loading_view);
            tvAdded = findViewById(R.id.tv_added);
        }

        @Override
        public void onBindView(int position) {
            ScanWirelessControllerBean item = getItem(position);
            if(item.getDeviceItems() != null && item.getDeviceItems().size() == 1){
                GlideApp.with(iconDevice).load(item.getDeviceItems().get(0).getImageUrl()).into(iconDevice);
                tvDeviceName.setText(item.getDeviceItems().get(0).getName());
            }else{
                GlideApp.with(iconDevice).load(item.getImageUrl()).into(iconDevice);
                tvDeviceName.setText(item.getName());
            }
            tvDeviceId.setText("ID:" + item.getDeviceSn());
            switch (item.getAddStatus()){
                case 0:
                    btnPreset.setVisibility(View.VISIBLE);
                    btnAdd.setVisibility(View.VISIBLE);
                    tvFailed.setVisibility(View.GONE);
                    btnRetry.setVisibility(View.GONE);
                    tvAdded.setVisibility(View.GONE);
                    loadingView.setVisibility(View.GONE);
                    break;
                case 1:
                    btnPreset.setVisibility(View.GONE);
                    btnAdd.setVisibility(View.GONE);
                    tvFailed.setVisibility(View.GONE);
                    btnRetry.setVisibility(View.GONE);
                    tvAdded.setVisibility(View.GONE);
                    loadingView.setVisibility(View.VISIBLE);
                    break;
                case 2:
                    btnPreset.setVisibility(View.GONE);
                    btnAdd.setVisibility(View.GONE);
                    tvFailed.setVisibility(View.GONE);
                    btnRetry.setVisibility(View.GONE);
                    tvAdded.setVisibility(View.VISIBLE);
                    loadingView.setVisibility(View.GONE);
                    break;
                case 3:
                    btnPreset.setVisibility(View.GONE);
                    btnAdd.setVisibility(View.GONE);
                    tvFailed.setVisibility(View.VISIBLE);
                    btnRetry.setVisibility(View.VISIBLE);
                    tvAdded.setVisibility(View.GONE);
                    loadingView.setVisibility(View.GONE);
                    break;
            }
        }
    }

    @Override
    public long getItemId(int position) {
        return getData().get(position).hashCode();
    }
}